bits 16

org 0x500

jmp main

;=====================================
;Code-Includes
;=====================================
%include "stdio.inc" 	;i/o routines
%include "gdt.inc"	;gdt routines
%include "A20.inc"	;A20 enabling routines
%include "Fat12.inc"	;Fat12 routines
%include "common.inc"
;=====================================
;Data-Section
;=====================================


loadMsg db "Preparing to load operating system...", 0x0D, 0x0A, 0x00
msgFailure db "Error occured...", 0x0D, 0x0A, 0x00

;=============================================
;STAGE2 ENTRY POINT
;
;-Store BIOS information
;-Load Kernel
;-Install GDT; go into protected mode (pmode)
;-Jump to Stage3
;=============================================


main:

	;=====================================
	;	Setup segments and stack
	;=====================================

	cli
	xor	ax, ax
	mov	ds, ax
	mov	es, ax
	mov	ax, 0x9000	; stack begins at 0x9000
	mov 	ss, ax
	mov	sp, 0xFFFF	; stack ends at 0xffff
	sti

	;=====================================
	;	Print loading msg
	;=====================================


	mov	si, loadMsg
	call	Puts16		;calls Puts16 in stdio.inc

	;=====================================
	;	Install our GDT
	;=====================================


	call InstallGDT		;call InstallGDT in gdt.inc

	;=====================================
	;	Enable A20 Address Line
	;=====================================

	call EnableA20_KKbrd_Out	;call EnableA20_KKbrd_Out in A20.inc

	;=====================================
	;	Initiate pmode
	;=====================================
	
	call	LoadRoot		;load root directory table

	mov	ebx, 0			;BX:BP points to buffer to load to
   	mov	ebp, IMAGE_RMODE_BASE
	mov	Esi, ImageName		;our file to load
	call	LoadFile		;load our file
	MOV	dword [ImageSize], ecx	;size of kernel
	cmp	ax, 0			;test for success
	je	EnterStage3		;load Stage3
	mov	si, msgFailure		;print error
	call	Puts16
	mov	ah, 0
	int     0x16                    ;await keypress
	int     0x19                    ;warm boot computer
	cli
	hlt

EnterStage3:

	cli
	mov 	eax, cr0
	or	eax, 1
	mov	cr0, eax
	
	jmp 	08h:Stage3
	
		
;=============================================
;STAGE3 ENTRY POINT
;=============================================
deploymentMsg db  0x0A, 0x0A, 0x0A, "        <OS Development Project 2009>"
    	db  0x0A, 0x0A,            "           Basic 32 bit graphics", 0

bits 32		;Now running in 32 bit!

Stage3:

	;=====================================
	;	Set Registers
	;=====================================
	
	mov	ax, 0x10	;set data segments to data selector
	mov	ds, ax	
	mov	es, ax
	mov	ss, ax
	mov	esp, 90000h 	;stack begins from 0x90000



CopyImage:		;Copy kernel to 1MB (0x10000)
			
  	mov	eax, dword [ImageSize]
  	movzx	ebx, word [bpbBytesPerSector]
  	mul	ebx
   	mov	ebx, 4
  	div	ebx
   	cld
   	mov    	esi, IMAGE_RMODE_BASE
   	mov	edi, IMAGE_PMODE_BASE
   	mov	ecx, eax
   	rep	movsd       ;copy image to its protected mode address

  	jmp	08h:IMAGE_PMODE_BASE; execute our kernel!


	;=====================================
	;   Clear screen and print success	
	;=====================================

	call		ClrScr32
	mov		ebx, deploymentMsg
	call		Puts32

;=============================================
;Stop execution
;=============================================

STOP:
	cli
	hlt


	